import sys
import os
import shutil
import argparse
import shell
import dbus

try:
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
except:
    print("PyQt5 not loading")
    exit()
    
def konfirmi():
    '''
    Printu tasko konfirmo.
    '''
    knotify = dbus.SessionBus().get_object("org.kde.knotify", "/Notify")
    titoli = "Project Planter"
    texto = "Project Created."
    knotify.event("warning", "kde", [], titoli, texto, [], ['Accept'], 0, 0, dbus_interface="org.kde.KNotify")

class Fenestro(QMainWindow):
    def __init__(self,opts,parent=None):
        #super(Fenestro, self).__init__(parent)
        QMainWindow.__init__(self, parent)
        central = QWidget()
        self.setCentralWidget(central)

        gridbox = QGridLayout(central)
        '''
        cssfile="etc/workflow/style.css"

        with open(cssfile,"r") as css:
            self.setStyleSheet(css.read())
        '''
        self.setWindowTitle("Project Planter")

        #fields
        self.echolabel = QLabel("Project Name")
        self.echo = QLineEdit(opts['echo'])
        self.echo.selectAll()

        self.locationlabel = QLabel("Location")
        self.location = QLineEdit(opts['destination'])
        self.locationbtn = QPushButton("")
        self.locationbtn.setIcon(QIcon('/usr/share/icons/oxygen/22x22/places/folder-blue.png'))
        self.locationbtn.setIconSize(QSize(22,22))
        self.locationbtn.clicked.connect(self.Projectbtn)

        self.sharedlabel = QLabel("Shared Directory")
        self.shared = QLineEdit(opts['shared'])
        self.sharedbtn = QPushButton("")
        self.sharedbtn.setIcon(QIcon('/usr/share/icons/oxygen/22x22/places/folder-blue.png'))
        self.sharedbtn.setIconSize(QSize(22,22))
        self.sharedbtn.clicked.connect(self.Sharedbtn)
        
        self.forlasibtn = QPushButton("Cancel")
        self.forlasibtn.clicked.connect(self.Forlasibtn)
        self.ekbtn = QPushButton("Create")
        self.ekbtn.clicked.connect(self.Ekbtn)
        self.opts = opts

        #layout
        gridbox.addWidget(self.echolabel, 0,0)
        gridbox.addWidget(self.echo, 1,0)
        gridbox.addWidget(self.locationlabel, 2,0)
        gridbox.addWidget(self.location, 3,0)
        gridbox.addWidget(self.locationbtn, 3,1)
        gridbox.addWidget(self.sharedlabel, 4,0)
        gridbox.addWidget(self.shared, 5,0)
        gridbox.addWidget(self.sharedbtn, 5,1)

        gridbox.addWidget(self.ekbtn, 6,0)
        gridbox.addWidget(self.forlasibtn, 6,1)

        self.setGeometry(200,200,450,250)
        self.preferoj = {}

    def Projectbtn(self):
        self.ARG = QFileDialog.getExistingDirectory(self, 'Destination', os.path.expanduser('~') )
        self.location.setText(str(self.ARG))

    def Sharedbtn(self):
        self.SHARED = QFileDialog.getExistingDirectory(self, 'Shared Directory', os.path.expanduser('~') )
        self.shared.setText(str(self.SHARED))

    def Ekbtn(self):
        self.opts['shared'] = str(self.shared.text() )
        self.opts['destination'] = str(self.location.text() )
        self.opts['echo'] = str(self.echo.text() )
        shell.execute(self.opts)
        self.close()

    def Forlasibtn(self):
        quit()

def execute(opts):
    plantergui = QApplication(sys.argv)
    window = Fenestro(opts)
    window.show()
    plantergui.exec_()
